const jwt = require('jsonwebtoken')

const key = "fuTkisMQQ2j1ESC0cbaQen1ZWmkMdvLx"
const expir = 60 * 60

//生成token
const setToken = function(rule) {
  let token = jwt.sign({rule},key,{ expiresIn: expir})
  return token
}
//验证token,返回解析后的规则
const verifyToken = function(token) {
  try {
    let tokenKey = jwt.verify(token,key)
    return {
      code:200,
      message:'校验成功',
      tokenKey:tokenKey
    }
  } catch (error) {
    return {
      code:401,
      message:'校验失败',
    }
  }
} 

module.exports = {
  setToken,
  verifyToken
}